﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SysManage_Others_SentEmailRecord : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 页面权限
            comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "SentEmailRecord" },
                Grv, new string[] { }, new int[] { }, CommonModel.loginOut2);
            QueryBind(0);
        }
    }

    #region 查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        QueryBind(1);
        HidQueryFlag.Value = "1";
    }
    //
    protected void QueryBind(int n)
    {
        StringBuilder StrSql = new StringBuilder();
        StrSql.Append(" select SER.*,U.UserName+'-'+U.UserEngName+'-'+U.UserFullName UserName ");
        StrSql.Append(" from M_SentEmailRecord SER left join B_User U on U.UserCode=SER.UserCode where SER.DeleteFlag=0 ");

        if (n == 1)
        {
            if (!string.IsNullOrEmpty(txtFromDate.Value) && !string.IsNullOrEmpty(txtToDate.Value))
            {
                StrSql.Append(string.Format(" and convert(datetime,EmailSentDate) between convert(datetime,'{0}')  and  convert(datetime,'{1}') ",
                    txtFromDate.Value, txtToDate.Value));
            }
            else if (!string.IsNullOrEmpty(txtFromDate.Value))
            {
                StrSql.Append(string.Format(" and convert(datetime,EmailSentDate)<convert(datetime,'{0}') ", txtFromDate.Value));
            }
            else if (!string.IsNullOrEmpty(txtToDate.Value))
            {
                StrSql.Append(string.Format(" and convert(datetime,EmailSentDate)<convert(datetime,'{0}') ", txtToDate.Value));
            }
            if (!string.IsNullOrEmpty(txtContractNo.Text))
            {
                StrSql.Append(string.Format(" and SER.ContractNoMain like '%{0}%'", txtContractNo.Text));
            }
            if (!string.IsNullOrEmpty(txtContractNoCustomer.Text))
            {
                StrSql.Append(string.Format(" and SER.ContractNoCust like '%{0}%'", txtContractNoCustomer.Text));
            }
            if (!string.IsNullOrEmpty(txtEmailTitle.Text))
            {
                StrSql.Append(string.Format(" and SER.EmailTitle like '%{0}%'", txtEmailTitle.Text));
            }
            if (!string.IsNullOrEmpty(txtEmailContent.Text))
            {
                StrSql.Append(string.Format(" and SER.EmailContent like '%{0}%'", txtEmailContent.Text));
            }
            if (!string.IsNullOrEmpty(txtUserCode.Text))
            {
                StrSql.Append(string.Format(" and SER.UserCode in (select distinct UserCode from B_User where UserName like '%{0}%'"
                    +" or UserEngName like '%{0}%' or UserFullName like '%{0}%')", txtUserCode.Text));
            }
            if (!ddlType.SelectedValue.Equals("-1"))
            {
                StrSql.Append(string.Format(" and SER.EmailType='{0}'", ddlType.SelectedValue));
            }
            if (!string.IsNullOrWhiteSpace(txtDeliveryApplyNo.Text))
            {
                StrSql.Append(string.Format(" and DeliveryApplyNo like '%{0}%'", txtDeliveryApplyNo.Text.Trim()));
            }
        }
        StrSql.Append(" order by EmailSentDate DESC ");
        Grv.Columns[Grv.Columns.Count - 2].Visible = true;
        DataTable Dt = help.GetDataTable(StrSql.ToString());
        Grv.DataSource = Dt;
        Grv.DataBind();
        Grv.Columns[Grv.Columns.Count - 2].Visible = false;

        if (Dt.Rows.Count > 0)
        {
            // 设置分页行永远显示
            Grv.BottomPagerRow.Visible = true;
            //赋值Label记录条数
            Label label1 = Grv.BottomPagerRow.FindControl("lblCount") as Label;
            label1.Text = Dt.Rows.Count.ToString();
        }
    }
    #endregion

    // 重置
    protected void cmdReset_Click(object sender, EventArgs e)
    {
        ddlType.SelectedIndex = 0;
        txtContractNoCustomer.Text = "";
        txtEmailContent.Text = "";
        txtToDate.Value = "";
        txtEmailTitle.Text = "";
        txtUserCode.Text = "";
        txtToPerson.Text = "";
    }

    //跳页
    protected void Grv_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "NewPage")
            {
                TextBox tb = Grv.BottomPagerRow.FindControl("txtNewPageIndex") as TextBox;
                int pageindex = Convert.ToInt32(tb.Text);
                Grv.PageIndex = pageindex - 1;
                //数据绑定
                QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
            }
        }
        catch
        {
            CommonMethod.regAlertMessage("输入不合法-Error format!", this);
        }
    }

    // 换页
    protected void Grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            Grv.PageIndex = e.NewPageIndex;
            QueryBind(Convert.ToInt16(HidQueryFlag.Value.ToString()));
        }
        catch
        { }
    }

    //protected void Grv_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    lblEmailContent.Text = Grv.Rows[Grv.SelectedIndex].Cells[Grv.Columns.Count - 2].Text.ToString();
    //}
    protected void Grv_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtToPerson.Text = Grv.DataKeys[Grv.SelectedIndex]["StrToPerson"].ToString();
        //CommonMethod.regAlertMessage(Grv.Rows[Grv.SelectedIndex].Cells[Grv.Columns.Count - 2].Text.ToString(), this);
    }
}